<template>
	<view class="search">
		<!-- #ifdef MP-WEIXIN -->
		<view class="search-navbar">
			<view class="input-view">
				<uni-icons class="input-uni-icon" type="search" size="24" color="#999" />
				<input confirm-type="search" v-model="keyWord" class="nav-bar-input" type="text" placeholder="商品搜索" />
			</view>
			<text @tap="search">搜索</text>
		</view>
		<!-- #endif -->
		<Line />
		<view class="search-item">
			<view class="search-title">
				<view class="f-color">最近搜索</view>
				<view class="iconfont icon-lajitong" @tap="clearHsitory"></view>
			</view>
			<view v-if="searchData.length">
				<view class="search-name f-color" v-for="(item,index) in searchData" :key="index" @tap="toSearchList(item)">{{item}}</view>
			</view>
			<view v-else>
				<view class="search-end">暂无搜索记录</view>
			</view>
		</view>
		<view class="search-item">
			<view class="search-title">
				<view class="f-color">热门搜索</view>
				<view class="iconfont icon-lajitong"></view>
			</view>
			<view>
				<view class="search-name f-color" @tap="toSearchList('大')">大</view>
				<view class="search-name f-color" @tap="toSearchList('小款衣服')">小款衣服</view>
			</view>
		</view>
	</view>
</template>

<script>
	import Line from "@/components/Line/Line.vue"
	export default {
		data() {
			return {
				//输入的关键词
				keyWord: "",
				//搜索过的词记录
				searchData:[]
			};
		},
		//监听标题栏搜索框内容变化状态
		onNavigationBarSearchInputChanged(e) {
			this.keyWord = e.text
		},
		//监听标题栏点击搜索
		onNavigationBarButtonTap(e) {
			this.search()
		},
		//监听软键盘的搜索按钮
		onNavigationBarSearchInputConfirmed() {
			this.search()
		},
		onLoad() {
			uni.getStorage({
				key:"searchData",
				success: res=> {
					this.searchData = JSON.parse(res.data)
				}
			})
			
		},
		components: {
			Line
		},
		methods: {
			search() {
				if (this.keyWord == "") {
					return uni.showToast({
						title: "关键词不能为空",
						icon: "none"
					})
				} else {
					uni.navigateTo({
						url: "/pages/search-list/search-list?keyWord="+this.keyWord
					})
				}
				uni.hideKeyboard()
				this.addSearch()
			},
			addSearch(){
				//防止搜索记录重复
				let idx = this.searchData.indexOf(this.keyWord)
				if(idx<0){
					//添加搜索记录
					this.searchData.unshift(this.keyWord)
				}else{
					this.searchData.unshift(this.searchData.splice(idx,1)[0])
				}
				//本地缓存
				uni.setStorage({
					key:"searchData",
					data:JSON.stringify(this.searchData)
				})
			},
			toSearchList(item){
				uni.navigateTo({
					url: "/pages/search-list/search-list?keyWord="+item
				})
			},
			//清除搜索记录
			clearHsitory(){
				uni.showModal({
					title:"重要提示",
					content:"是否要清除搜索记录",
					success:(res)=> {
						//点击确定按钮
						if(res.confirm){
							this.searchData = []
							//uni.clearStorage() 清除所有本地缓存
							uni.removeStorage({//清除指定key
								key:"searchData"
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	/* #ifdef MP-WEIXIN */
	.search-navbar{
		display: flex;
		align-items: center;
		justify-content: space-between;
		.input-view {
			display: flex;
			align-items: center;
			border-radius: 30rpx;
			width: 83%;
			height: 64rpx;
			margin: 10rpx 0;
			background-color: rgb(247, 247, 247);
			box-sizing: border-box;
			padding: 0 20rpx;
			margin-left: 20rpx;
			.nav-bar-input {
				margin-left: 20rpx;
			}
		}
		text{
			margin-right: 20rpx;
		}
	}
	
	/* #endif */
	.search-item {
		padding: 20rpx;

		.search-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
		}

		.search-name {
			padding: 4rpx 24rpx;
			background-color: #E1E1E1;
			display: inline-block;
			border-radius: 26rpx;
			margin: 10rpx;
		}
		.search-end{
			text-align: center;
		}
	}
</style>
